package com.mesSystem.service.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mesSystem.entity.Employee;
import com.mesSystem.entity.Equipment;
import com.mesSystem.entity.WorkstationAndEmployee;
import com.mesSystem.entity.WorkstationAndEquipment;
import com.mesSystem.mapper.WorkstationAndEquipmentMapper;
import com.mesSystem.service.impl.EquipmentService;
import com.mesSystem.service.impl.WorkstationAndEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class WorkstationAndEquipmentServiceImpl extends ServiceImpl<WorkstationAndEquipmentMapper, WorkstationAndEquipment> implements WorkstationAndEquipmentService {

    @Autowired
    EquipmentService equipmentService;

    /**
     * 更新工位的设备，接受到的设备id集合中，
     * 如果已经存在于表中，则删除。
     * 如果未存在，则添加。
     *
     * @param equipmentIds  设备id的list集合
     * @param workstationId 工位id
     */
    public void update(List<Long> equipmentIds, Long workstationId) {
        // 创建一个WorkstationAndEquipment对象用来对比数据库信息
        WorkstationAndEquipment workstationAndEquipment = new WorkstationAndEquipment();
        workstationAndEquipment.setWorkstationId(workstationId);
        for (Long equipmentId : equipmentIds) {
            workstationAndEquipment.setEquipmentId(equipmentId);
            // 如果设备id已存在，则删除
            if (count(new QueryWrapper<>(workstationAndEquipment)) > 0) {
                remove(new QueryWrapper<>(workstationAndEquipment));
            } else {
                // 如果未存在，则添加新的设备id
                save(workstationAndEquipment);
            }
        }
    }

    /**
     * 分页查询通返回不在其他岗位上的工位的信息（在本岗位或无岗位）
     *
     * @param page
     * @param size
     * @param workstationId
     * @return
     */
    public List<Equipment> queryAvailableEquipment(Integer page, Integer size, Long workstationId) {
        // 创建分页对象
        Page<Equipment> equipmentPage = new Page<>(page, size);

        // 构造查询条件
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        // 获得所有员工信息，但剔除（非workstationId对应）不在本工位的，在其他岗位上的员工信息
        queryWrapper.ne("equipment_id", getEquipmentIdsNonMatchingWorkstationId(workstationId));

        // 执行分页查询
        equipmentService.page(equipmentPage, queryWrapper);
        List<Equipment> equipments = equipmentPage.getRecords();

        return equipments;
    }

    /**
     * 获得在工位-设备表中，不对应于workstationId的设备id
     *
     * @param workstationId
     * @return
     */
    private List<Long> getEquipmentIdsNonMatchingWorkstationId(Long workstationId) {
        // 在这里查询工位-设备表中的设备ID，根据工位ID筛选出不对应于工位id的设备id
        QueryWrapper<WorkstationAndEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.notIn("workstation_id", Collections.singletonList(workstationId));
        List<WorkstationAndEquipment> equipments = list(queryWrapper);
        if (equipments == null) {
            equipments = new ArrayList<>();
        }
        return equipments.stream()
                .map(WorkstationAndEquipment::getEquipmentId)
                .collect(Collectors.toList());
    }

    /**
     * 根据工位id查询工位-设备表中的设备信息
     *
     * @param page
     * @param size
     * @param workstationId
     * @return 设备id + 设备名称
     */
    public List<Equipment> queryEquipmentByWorkstationId(Integer page, Integer size, Long workstationId) {
        // 创建分页器对象
        Page<WorkstationAndEquipment> pages = new Page<>(page, size);

        // 创建查询条件，根据 workstationId 查询
        QueryWrapper<WorkstationAndEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("workstation_id", workstationId);

        // 调用分页查询方法，将分页器和查询条件传递给 service
        page(pages, queryWrapper);

        // 从分页器对象中获取分页后的数据
        List<WorkstationAndEquipment> workstations = pages.getRecords();

        // 提取设备ID和姓名并返回
        List<Equipment> equipmentList = workstations.stream()
                .map(workstationAndEquipment -> {
                    Long equipmentId = workstationAndEquipment.getEquipmentId();
                    String equipmentName = equipmentService.getEquipmentName(equipmentId); // 使用之前提到的方法查询设备姓名
                    return new Equipment(equipmentId, equipmentName);
                })
                .collect(Collectors.toList());

        // 返回分页结果
        return equipmentList;
    }


}
